from time import sleep

from qturboframe.lib.report.log import logger
from qturboframe.lib.utils.check import CheckBasic
from qturboframe.lib.webdriver.exceptions import SeleniumError

from .base_element import BaseElement


class Tab(BaseElement, CheckBasic):
    '''
    jres2.0的Tab操作

    :Attributes:
     - _driver: 控件所在的当前webdriver对象
     - _element: 控件元素webelement对象
     - timeout: 步骤操作的超时时间

    Raises:
        当以文本找不到对应button时则抛出SeleniumError异常

    :Example:
     - 例1: Tab(webelement)
     - 例2: Tab({By.NAME:'name'})
     - 例3: Tab({By.CLASS_NAME:'class_name','text':'文本信息'})
     - 例4: Tab({By.CLASS_NAME:'class_name','text':'文本信息'}, father=driver)
     - 例5: Tab({By.CLASS_NAME:'class_name','text':'文本信息'}, father=webelement)
    '''

    def __init__(self, element_para, father=None, timeout=5):
        super().__init__(element_para, father, timeout)

    def get_all_tabs(self):
        '''
        jires2.0获取所以已打开的tab

        :Returns:
            已经打开的tab页

        :Example:
         - 例: tab.get_all_tabs()
        '''
        locator = ".//div[@class='h-screen-tab-con']/ul/li[not(contains(@style,'display: none'))]"
        tabs = self._element.find_elements_by_attribute("xpath", locator, timeout=self.timeout)
        tab_text_list = []
        for tab in tabs:
            tab_text_list.append(BaseElement(tab).get_text())

        return tab_text_list

    def get_tab(self, text):
        '''
        jires2.0跳转到某一个已经打开的tab

        :Returns:
            无

        :Example:
         - 例: tab.get_tab("棋牌合约停牌展期")
        '''
        locator = ".//div[@class='h-screen-tab-con']/ul/li[not(contains(@style,'display: none'))]"
        tabs = self._element.find_elements_by_attribute("xpath", locator, timeout=self.timeout)
        tab_text_list = []
        for tab in tabs:
            if text in BaseElement(tab).get_text():
                BaseElement(tab).click()
                break
        else:
            raise SeleniumError("已打开的tab中没有此'{}'tab".format(text))

    
    def close_current_open_tab(self):
        '''
        jres2.0 - 关于当前打开tab

        :Example:
         - 例: tab.close_current_open_tab()
        '''
        try:
            css_selector = 'div.h-screen-tab-set[title="视图工具栏"]'
            closediv = self._element.find_element_by_attribute('css selector', css_selector,timeout=self.timeout)
            sleep(1)
            BaseElement(closediv).click()
            ul = closediv.exist("xpath", './ul', timeout=self.timeout)
            if ul:
                BaseElement(ul.find_element_by_attribute('link text', '关闭当前选项卡', timeout=self.timeout)).click()
            else:
                raise SeleniumError("没有找到‘关闭当前选项卡’按钮")
                
        except Exception as msg:
            logger.warn(str(msg))
            raise SeleniumError(str(msg))

    def close_all_opened_tabs(self):
        '''
        jres2.0 - 关闭当前打开的所有tab
        '''
        try:
            css_selector = 'div.h-screen-tab-set[title="视图工具栏"]'
            closediv = self._element.find_element_by_attribute('css selector', css_selector, timeout=self.timeout)
            sleep(1)
            BaseElement(closediv).click()
            ul = closediv.exist("xpath", './ul', timeout=self.timeout)
            if ul:
                BaseElement(ul.find_element_by_attribute('link text', '关闭全部选项卡', timeout=self.timeout)).click()
            else:
                raise SeleniumError("没有找到‘关闭全部选项卡’按钮")

        except Exception as msg:
            logger.warn(str(msg))
            raise SeleniumError(str(msg))

    def close_other_opened_tabs(self):
        '''
        jres2.0 - 关闭当前打开的所有tab
        '''
        try:
            css_selector = 'div.h-screen-tab-set[title="视图工具栏"]'
            closediv = self._element.find_element_by_attribute('css selector', css_selector, timeout=self.timeout)
            sleep(1)
            BaseElement(closediv).click()
            ul = closediv.exist("xpath", './ul', timeout=self.timeout)
            if ul:
                BaseElement(ul.find_element_by_attribute('link text', '关闭当前以外选项卡', timeout=self.timeout)).click()
            else:
                raise SeleniumError("没有找到‘关闭当前以外选项卡’按钮")

        except Exception as msg:
            logger.warn(str(msg))
            raise SeleniumError(str(msg))
